package game;

import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.TiledLayer;
import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.control.ToneControl;

import enemies.Dragon;
import enemies.Rock;
import enemies.Tom;


/**
 * Game Class
 * 
 * @author Group C03
 * @author Mona Khlousy
 * @author Heba El-Samahi 
 * @author Daniel Schobert
 * 
 * @since 18.02.2009
 */

public class LUMM2_Game {
                
        /** gamestate for choosing sound/volume/language etc. */
        public final static byte SETTINGS = 0;

        /** gamestate for playing the into video with skip function */
        public final static byte INTRO = 1;

        /** gamestate for displaying the SVG menu */
        public final static byte MENU = 2;

        /** state while playing the game */
        public final static byte PLAY = 3;

        /** gamestate for playing the winner ceremony */
        public final static byte WIN = 4;

        /** state for game over actions */
        public final static byte GAME_OVER = 5;
        
        /**state for showing the highscore*/
        public final static byte HIGHSCORE = 6;

        /** current game state, start with Menu **/
        public static byte currentGameState;
        
        /** last game state*/
        public static byte lastGameState;
        
        /** layer manager */
        public static LayerManager layerManager;
        
        /** level background*/
        public static TiledLayer background;
        public static int[] cells;
        
        /** dug Sprite */
        private static Sprite dugSprite;
        private static Sprite pumpeSprite;
        public static int posX;
        public static int posY;
        private static int dugFrame;
        
        /** number of lives which are left (dug - the player) */
        public static int dugLives;
        
        /** score */
        public static int score;
        
        /** laufvariable fuer lose-screen*/
        private static int lose = 0;
        
        /** dragon-monster image*/
        private static Image dragon;
        private static Image dragonDieing;
        
        /** tom-monster image*/
        private static Image tom;
        private static Image tomDieing;
        
        /** dragon-moster sprite 1 */
        private static Dragon dragon1;
        public static Sprite dragon1Sprite;
        public static Sprite d1_dieingSprite;
        
        /** dragon-moster sprite 2 */
        private static Dragon dragon2;
        public static Sprite dragon2Sprite;
        public static Sprite d2_dieingSprite;
        
        /** tom-moster sprite 1 */
        private static Tom tom1;
        public static Sprite tom1Sprite;
        public static Sprite t1_dieingSprite;
        
        /** tom-moster sprite 2 */
        private static Tom tom2;
        public static Sprite tom2Sprite;
        public static Sprite t2_dieingSprite;
        
        /** rock sprite 1 */
        private static Rock rock1;
        
        /**for moving and fire */
        private static byte move = 0;
        private static final byte moveUp = 1;
        private static final byte moveDown = 2;
        private static final byte moveRight = 3;
        private static final byte moveLeft = 4;
        private static final byte fire = 5;
        private static int i = 0;
        
        /** for beating enemie*/
        private static boolean catchedD1 = false;
        private static boolean catchedT1 = false;
        private static boolean catchedD2 = false;
        private static boolean catchedT2 = false;
        
        /** for looking direction */
        private static byte look = moveRight;
        
        /** tone for moving */
        private static byte tone = ToneControl.C4;
        private static int j=0;
        public static boolean mute=false;
        
        /**aktuelle zelle des hintergrundes*/
        private static int currentRow;
        private static int currentCol;
        private static int lowerRow;
        private static int rightCol;
        
        /**counter fuer besiegte gegner*/
        private static int deathCounter =0;
        
        /**Name fuer den Highscore*/
        private static char[] playerName ={'a','a','a','a','a','a','a'};
        private static char[] active = {'-',' ',' ',' ',' ',' ',' '};
        private static int buchstabe1 = 1;
        private static int buchstabeNr = 0;
        private static char tmp1 ='a';
        
        /**boolean fuer pausenzustand*/
        public static boolean pause = false;
        private static Image pauseImg;
        private static Image gameoverImg;
        public static LUMM2_records rec = new LUMM2_records();
    
        /**
         * setzt alle variablen auf anfangszustand
         * Ladet alle fuer das Spiel benoetigten bilder und erstellt alle sprites
         * setzt Level auf 1
         * fuellt den highscore
         * laesst den Hintergrund erstellen
         * 
         * @param initGameState
         * 			der Zustand in dem das Spiel am Anfang sein soll
         */
        public final static void init(byte initGameState) {
                currentGameState = initGameState;
                lastGameState = currentGameState;

                layerManager = new LayerManager();
                LUMM2_Level.level=1;
                Image bgImage = loadImage("/tiles/tiles.png");  
                pauseImg = loadImage("/splash/pause.png");
                gameoverImg = loadImage("/splash/game_over.png");
                background = new TiledLayer(8,9, bgImage, 30,30);
                Image dug = loadImage("/sprites/dug.png");
                dugSprite = new Sprite(dug, 30, 30);
                posX=0;
                posY=0;
                dugFrame = 0;
                dugLives = 3; //zu beginn hat man 3 Leben
                score = 0; //zu beginn ist score 0 logischerweise
                dugSprite.setPosition(posX, posY);
                dugSprite.setFrame(dugFrame);
                layerManager.append(dugSprite);
                
                Image pumpe= loadImage("/sprites/pumpe.png");
                pumpeSprite = new Sprite(pumpe, 35, 30);
        		pumpeSprite.setPosition(1000,1000);
        		layerManager.append(pumpeSprite);
                
                dragon = loadImage("/sprites/drache.png");
                tom = loadImage("/sprites/tommonster.png");
                dragonDieing = loadImage("/sprites/dragon_dieing.png");
                tomDieing = loadImage("/sprites/tom_dieing.png");
                
                dragon1Sprite = new Sprite(dragon, 30, 30);
                dragon1Sprite.setPosition(700, 700);
                d1_dieingSprite = new Sprite(dragonDieing, 45, 45);
                d1_dieingSprite.setPosition(700, 700);
                tom1Sprite = new Sprite(tom, 30, 30);
                tom1Sprite.setPosition(700, 700);
                t1_dieingSprite = new Sprite(tomDieing, 45, 40);
                t1_dieingSprite.setPosition(700, 700);
                tom2Sprite = new Sprite(tom, 30,30);
                tom2Sprite.setPosition(700, 700);
                t2_dieingSprite = new Sprite(tomDieing, 45, 40);
                t2_dieingSprite.setPosition(700, 700);
                dragon2Sprite = new Sprite(dragon, 30,30);
                dragon2Sprite.setPosition(700, 700);
        		d2_dieingSprite = new Sprite(dragonDieing, 45, 45);
        		d2_dieingSprite.setPosition(700, 700);
        		layerManager.append(dragon1Sprite);
        		layerManager.append(dragon2Sprite);
        		layerManager.append(tom1Sprite);
        		layerManager.append(tom2Sprite);
        		layerManager.append(d1_dieingSprite);
        		layerManager.append(d2_dieingSprite);
        		layerManager.append(t1_dieingSprite);
        		layerManager.append(t2_dieingSprite);             
        
                createBackground();
                layerManager.append(background);
                
                
                fillHighscores();  
        }

        /**
         * draw the game
         * @currentGameState  SETTINGS:
         * 				der Hilfe Dialog wird gezeichnet
         * @currentGameState  PLAY:
         * 				das level, der spieler und die gegner werden gezeichnet und bewegt
         * 				(spiellogik)
         * @currentGameState  WIN:
         * 				Winner Ceremony wird gezeichnet, bei der man den namen fuer den highscore eingeben kann
         * @currentGameState  GAME OVER:
         * 				Game Over Splashscreen wird gezeichnet und nach kurzer zeit kommt der Highscore
         * @currentGameState  HIGHSCORE:
         * 				der Highscore wird gezeichnet
         * @param g
         *            Graphic Object
         */

        public final static void drawGame(Graphics g) {
                LUMM2_Canvas.clearScreen(255, 255, 255, g);

                
                switch (currentGameState) {
                case SETTINGS:
                	g.setColor(0);
                	g.fillRect(0, 0, LUMM2_Canvas.canvasWidth, LUMM2_Canvas.canvasHeight);
                	g.setColor(255,255,255);
                	g.drawString("H E L P", 90, 30, 0);
                	g.drawString("navigation..... arrow keys ",10, 80, 0);
                	g.drawString("      or '2','4','6' and '8'", 10, 100, 0);
                	g.drawString("PAUSE ......... press '1'", 10, 120, 0);
                	g.drawString("SOUND on/off... press '3'", 10, 140, 0);
                	g.drawString("kill enemy..... press 'enter'", 10, 160, 0);
                	g.drawString("                     or '5' ", 10, 180, 0);
                    break;
                case INTRO:
                    break;
                case MENU:
                	break;
                case PLAY:
                	g.fillRect(0, 0, LUMM2_Canvas.canvasWidth, LUMM2_Canvas.canvasHeight);
                	if(!pause){
                		for (int i = 0; i< layerManager.getSize(); i++){
                			layerManager.getLayerAt(i).setVisible(true);
                		}
                        switch(LUMM2_Level.level){
                        case 1:
                        	if(deathCounter ==2) resetLevel();
                        	break;
                        case 2:
                        	if(deathCounter ==3) resetLevel();
                        	break;
                        case 3:
                        	if(deathCounter ==4) resetLevel();
                        	break;                      	
                        }
                        //Berechnet die aktuelle zeile und spalte im hintergrund
                        currentRow = posY*9/270;
                        currentCol = posX*8/240;
                        lowerRow = posY*9/270+1;
                        rightCol = posX*8/240+1;
                        
                        calculatePosition();
                        moveDug(currentRow, currentCol, lowerRow, rightCol);
                        switch(LUMM2_Level.level){
                        case 1:
                                dragon1.moveEnemie();
                                tom1.moveEnemie();
                                break;
                        case 2:
                                dragon1.moveEnemie();
                                tom1.moveEnemie();
                                tom2.moveEnemie();
                                break;
                        case 3:
                                dragon1.moveEnemie();
                                tom1.moveEnemie();
                                tom2.moveEnemie();
                                dragon2.moveEnemie();
                                break;
                        }
                	} else  {
                		//layerManager.append(splashscreen);
                		g.setColor(0);
                		g.fillRect(0, 0, LUMM2_Canvas.canvasWidth, LUMM2_Canvas.canvasHeight);
                		g.setColor(255, 255, 255);
                		for (int i = 0; i< layerManager.getSize(); i++){
                			layerManager.getLayerAt(i).setVisible(false);
                		}
                		
                		g.drawImage(pauseImg, 0, 0, 0);
                		//g.drawString("PAUSE \npress 1 to continue", 100, 100, 0);
                		
                		layerManager.setViewWindow(0, 0, LUMM2_Canvas.canvasWidth, LUMM2_Canvas.canvasHeight);
                        //g.setColor(0);
                        layerManager.paint(g, 0, 0);
                        
                		//g.drawString("PAUSE", 100, 285, 0);
                		
                	}

                    layerManager.setViewWindow(0, 0, LUMM2_Canvas.canvasWidth, LUMM2_Canvas.canvasHeight);
                    layerManager.paint(g, 0, 0);
                    
                    g.setColor(0);
                    g.drawString("Score: "+score, 0, 270, 0);
                    g.drawString("Lives: "+dugLives, 0, 285, 0);
                    if (!mute){
                    	g.fillTriangle(135, 275, 147, 275, 141, 295);
                    	g.drawString("sound ON", 150, 278, 0);
                    }
                    else {
                    	g.drawLine(135, 275, 147, 275);
                    	g.drawLine(147, 275, 141, 295);
                    	g.drawLine(141, 295, 135, 275);
                    	g.drawString("sound OFF", 150, 278, 0);
                    }
                        break;
                case WIN:
                	lastGameState = WIN;
                	g.setColor(0);
                    g.fillRect(0, 0, LUMM2_Canvas.canvasWidth,  LUMM2_Canvas.canvasHeight);
                    g.setColor(255,255,255);
                    g.drawString("      Congratulations!!\n       You've won! \n\n\n\nPlease enter your name:", 10, 60, 0);
                    
                    if (LUMM2_Canvas.up){
                    	buchstabe1 ++;
                    } 
                    if (LUMM2_Canvas.down){
                    	buchstabe1 --;
                    }
                    if (LUMM2_Canvas.right){
                    	if (buchstabeNr <=5){
                    		buchstabe1 = 0;
                    		tmp1 = 'a';
                    		buchstabeNr ++;
                    	}
                    }
                    
                    switch (buchstabe1){
                	case 1: 
                		tmp1 ='a';
                		break;
                	case 2: 
                		tmp1 ='b';
                		break;
                	case 3: 
                		tmp1 ='c';
                		break;
                	case 4: 
                		tmp1 ='d';
                		break;
                	case 5: 
                		tmp1 ='e';
                		break;
                	case 6: 
                		tmp1 ='f';
                		break;
                	case 7: 
                		tmp1 ='g';
                		break;
                	case 8:
                		tmp1 ='h';
                		break;
                	case 9: 
                		tmp1 ='i';
                		break;
                	case 10: 
                		tmp1 ='j';
                		break;
                	case 11: 
                		tmp1 ='k';
                		break;
                	case 12: 
                		tmp1 ='l';
                		break;
                	case 13: 
                		tmp1 ='m';
                		break;
                	case 14: 
                		tmp1 ='n';
                		break;
                	case 15: 
                		tmp1 ='o';
                		break;
                	case 16: 
                		tmp1 ='p';
                		break;
                		
                	case 17: 
                		tmp1 ='q';
                		break;
                	case 18: 
                		tmp1 ='r';
                		break;
                	case 19: 
                    	tmp1 ='s';
                    	break;
                	case 20: 
                    	tmp1 ='t';
                    	break;
                	case 21: 
                    	tmp1 ='u';
                    	break;
                	case 22: 
                    	tmp1 ='v';
                    	break;
                	case 23: 
                    	tmp1 ='w';
                    	break;
                	case 24: 
                    	tmp1 ='x';
                    	break;
                	case 25: 
                    	tmp1 ='y';
                    	break;
                	case 26: 
                    	tmp1 ='z';
                    	break;
                	}
                	playerName[buchstabeNr] = tmp1;
                	for(int t=0; t<active.length; t++){
                		if (buchstabeNr == t) active [t] = '-';
                		else active[t] = ' ';
                	}
                 	g.drawString(String.valueOf(playerName), 10, 180, 0);
                 	g.drawString(String.valueOf(active), 10, 187, 0);
                 	if (LUMM2_Canvas.fire){
                    	rec.addScore(score, String.valueOf(playerName));
                   		currentGameState = HIGHSCORE;
                    	reset();
                    }

                    break;
                case GAME_OVER:
                	if(lose>0){
//                		g.setColor(0);
//                		g.fillRect(0, 0, LUMM2_Canvas.canvasWidth,  LUMM2_Canvas.canvasHeight);
//                		g.setColor(255,255,255);
                		g.drawImage(gameoverImg, 0, 0, 0);
                		
                     	//g.drawString("YOU LOSE!!!", 10, 60, 0);
                      	lose--;
                	}
                	else{
                		LUMM2_Midlet.flashBacklight(500);
                       	currentGameState = HIGHSCORE;
                	}
                    break;
                case HIGHSCORE:
                	lastGameState = MENU;
                    g.setColor(0);
                    //printScores(g);
                    rec.printScores(g);
                    break;
                }
        }
        
        /**
         * setzt alle Variablen und sprites auf den anfangsstand
         * */
        public static void reset() {
        	LUMM2_Level.level = 1;
        	for (int z=0; z< playerName.length; z++){
        		playerName[z] ='a';
        		active[z] = ' ';
        	}
        	active [0] = '-';
        	tmp1 = 'a';
        	buchstabeNr = 0;
        	buchstabe1 = 1;
        	score = 0;
        	dugLives = 3;
        	posX = 0;
    		posY = 0;
    		dugFrame = 0;
    		dugSprite.setTransform(Sprite.TRANS_NONE);
    		dugSprite.setPosition(posX, posY);
    		dugSprite.setFrame(dugFrame);
    		move = 0;
    		look = moveRight;
    		i=0;
    		deathCounter=0;
            dragon1Sprite.setPosition(700, 700);
            d1_dieingSprite.setPosition(700, 700);
            tom1Sprite.setPosition(700, 700);
            t1_dieingSprite.setPosition(700, 700);
            tom2Sprite.setPosition(700, 700);
            t2_dieingSprite.setPosition(700, 700);
            dragon2Sprite.setPosition(700, 700);
    		d2_dieingSprite.setPosition(700, 700);
    		
    		createBackground();
    		createEnemies(LUMM2_Level.level);     
        }
        
        /**
         * setzt alles auf den anfangszustand und erhoeht das Level
         * */
        private static void resetLevel(){
        	LUMM2_Level.level++;
        	if(LUMM2_Level.level<4){
        		posX = 0;
        		posY = 0;
        		dugFrame = 0;
        		dugSprite.setFrame(dugFrame);
        		dugSprite.setTransform(Sprite.TRANS_NONE);
        		dugSprite.setPosition(posX, posY);
        		move = 0;
        		look = moveRight;
        		i=0;
        		deathCounter=0;
        		
        		dragon1Sprite.setPosition(700, 700);
                d1_dieingSprite.setPosition(700, 700);
                tom1Sprite.setPosition(700, 700);
                t1_dieingSprite.setPosition(700, 700);
                tom2Sprite.setPosition(700, 700);
                t2_dieingSprite.setPosition(700, 700);
                dragon2Sprite.setPosition(700, 700);
        		d2_dieingSprite.setPosition(700, 700);
            
        		createBackground();
        		createEnemies(LUMM2_Level.level);
        	}
        	else{
        		score=score+(dugLives*200);
        		currentGameState = WIN;
        	}
        }

        /**
         * Methode zum bewegen des Spielers
         * und wenn feuertaste gedruckt wird, zum stunnen und besiegen des gegners
         * 
         * @param currentRow
         * 			aktuelle Reihe des hintergrundes in der der spieler sich befindet
         * @param currentCol
         * 			aktuelle Spalte des hintergrundes in der der spieler sich befindet
         * @param lowerRow
         * 			reihe des hintergrundes, unter dem Spieler
         *  @param rightCol
         * 			Spalte des hintergrundes, rechts vom Spieler
         * */
        private static void moveDug(int currentRow, int currentCol, int lowerRow, int rightCol){
                
                if(i>0 && move == moveUp ){
                        posY = posY-6;
                        if(posY%30==0){ //wenn die spielfigur in einer zeile des hintergrundes ist                              
                                if(background.getCell(currentCol, currentRow)==9 || background.getCell(currentCol, currentRow)==12){
                                        if(background.getCell(currentCol, lowerRow)!=8){
                                                background.setCell(currentCol, lowerRow, 9);
                                        }
                                }
                                else{
                                        if(background.getCell(currentCol, lowerRow)==8){
                                                background.setCell(currentCol, currentRow, 10);
                                                score += 2;
                                        }
                                        else{
                                                background.setCell(currentCol, currentRow, 10);
                                                background.setCell(currentCol, lowerRow, 9);
                                                score += 2;
                                        }
                                }                                       
                        }
                        if(i%3 == 0)dugFrame = (dugFrame+1) % 2;
                        dugSprite.setPosition(posX, posY);
                        dugSprite.setFrame(dugFrame);
                        i--;
                }
                else if(i>0 && move==moveDown ){
                        posY = posY +6;
                        if(posY%30==0){
                                if(background.getCell(currentCol, lowerRow)==9 || background.getCell(currentCol, lowerRow)==12){
                                        if(background.getCell(currentCol, currentRow)!=10){
                                                background.setCell(currentCol, currentRow, 9);
                                        }
                                }
                                else{
                                        if(background.getCell(currentCol, currentRow)==10){
                                                background.setCell(currentCol, lowerRow, 8);
                                                score += 2;
                                        }
                                        else{
                                                background.setCell(currentCol, lowerRow, 8);
                                                background.setCell(currentCol, currentRow, 9);
                                                score += 2;
                                        }
                                }
                        }
                        if(i%3 == 1)dugFrame = (dugFrame+1) % 2;
                        dugSprite.setPosition(posX, posY);
                        dugSprite.setFrame(dugFrame);
                        i--;
                }
                else if(i>0 && move == moveRight ){
                        posX = posX +6;
                        if(posX%30==0){
                                if(background.getCell(rightCol, currentRow)==12 || background.getCell(rightCol, currentRow)==9){
                                        if(background.getCell(currentCol, currentRow) != 11){
                                                background.setCell(currentCol, currentRow, 12);
                                        }
                                }
                                else{
                                        if(background.getCell(currentCol, currentRow)==11){
                                                background.setCell(rightCol, currentRow, 13);
                                                score += 2;
                                        }
                                        else{
                                                background.setCell(rightCol, currentRow, 13);
                                                background.setCell(currentCol, currentRow, 12);
                                                score += 2;
                                        }
                                }
                        }
                        if(i%3 == 1)dugFrame = (dugFrame+1) % 2;
                        dugSprite.setPosition(posX, posY);
                        dugSprite.setFrame(dugFrame);
                        i--;
                }
                else if(i>0 && move == moveLeft){
                        posX = posX -6;
                        if(posX%30==0){
                                if(background.getCell(currentCol, currentRow)==12 || background.getCell(currentCol, currentRow)==9){
                                        if(background.getCell(rightCol, currentRow)!= 13){
                                                background.setCell(rightCol, currentRow, 12);
                                        }
                                }
                                else{
                                        if(background.getCell(rightCol, currentRow)==13){
                                                background.setCell(currentCol, currentRow, 11);
                                                score += 2;
                                        }
                                        else{
                                                background.setCell(currentCol, currentRow, 11);
                                                background.setCell(rightCol, currentRow, 12);
                                                score += 2;
                                        }
                                }
                        }
                        if(i%3 == 1)dugFrame = (dugFrame+1) % 2;
                        dugSprite.setPosition(posX, posY);
                        dugSprite.setFrame(dugFrame);
                        i--;
                }
                else if(i>0 && move == fire ){
                	switch(LUMM2_Level.level){
                	case 1:
                		if(pumpeSprite.collidesWith(dragon1Sprite, true) && !catchedD1){
                			catchedD1=true;
                			if(dragon1.isStunned()){
                				dragon1.setPumped();
                			}
                			dragon1.setStunned(40);
                		}
                		else if(pumpeSprite.collidesWith(tom1Sprite, true) && !catchedT1){
                			catchedT1=true;
                			if(tom1.isStunned()){
                				tom1.setPumped();
                			}
                			tom1.setStunned(40);
                		}
                		else if(pumpeSprite.collidesWith(d1_dieingSprite, true)&& !catchedD1){
                			catchedD1=true;
                			dragon1.setPumped();
                		}
                		else if(pumpeSprite.collidesWith(t1_dieingSprite, true)&& !catchedT1){
                			catchedT1=true;
                			tom1.setPumped();
                		}
                		break;
                	case 2:
                		if(pumpeSprite.collidesWith(dragon1Sprite, true) && !catchedD1){
                			catchedD1=true;
                			if(dragon1.isStunned()){
                				dragon1.setPumped();
                			}
                			dragon1.setStunned(40);
                		}
                		else if(pumpeSprite.collidesWith(tom1Sprite, true) && !catchedT1){
                			catchedT1=true;
                			if(tom1.isStunned()){
                				tom1.setPumped();
                			}
                			tom1.setStunned(40);
                		}
                		else if(pumpeSprite.collidesWith(tom2Sprite, true)&&!catchedT2){
                			catchedT2=true;
                			if(tom2.isStunned()){
                				tom2.setPumped();
                			}
                			tom2.setStunned(40);
                		}
                		else if(pumpeSprite.collidesWith(d1_dieingSprite, true)&& !catchedD1){
                			catchedD1=true;
                			dragon1.setPumped();
                		}
                		else if(pumpeSprite.collidesWith(t1_dieingSprite, true)&& !catchedT1){
                			catchedT1=true;
                			tom1.setPumped();
                		}
                		else if(pumpeSprite.collidesWith(t2_dieingSprite, true)&& !catchedT2){
                			catchedT2=true;
                			tom2.setPumped();
                		}
                		break;
                	case 3:
                		if(pumpeSprite.collidesWith(dragon1Sprite, true)&&!catchedD1){
                			catchedD1=true;
                			if(dragon1.isStunned()){
                				dragon1.setPumped();
                			}
                			dragon1.setStunned(40);
                		}
                		else if(pumpeSprite.collidesWith(tom1Sprite, true)&&!catchedT1){
                			catchedT1=true;
                			if(tom1.isStunned()){
                				tom1.setPumped();
                			}
                			tom1.setStunned(40);
                		}
                		else if(pumpeSprite.collidesWith(tom2Sprite, true)&&!catchedT2){
                			catchedT2=true;
                			if(tom2.isStunned()){
                				tom2.setPumped();
                			}
                			tom2.setStunned(40);
                		}
                		else if(pumpeSprite.collidesWith(dragon2Sprite, true)&&!catchedD2){
                			catchedD2=true;
                			if(dragon2.isStunned()){
                				dragon2.setPumped();
                			}
                			dragon2.setStunned(40);
                		}
                		else if(pumpeSprite.collidesWith(d1_dieingSprite, true)&& !catchedD1){
                			catchedD1=true;
                			dragon1.setPumped();
                		}
                		else if(pumpeSprite.collidesWith(d2_dieingSprite, true)&& !catchedD2){
                			catchedD2=true;
                			dragon2.setPumped();
                		}
                		else if(pumpeSprite.collidesWith(t1_dieingSprite, true)&& !catchedT1){
                			catchedT1=true;
                			tom1.setPumped();
                		}
                		else if(pumpeSprite.collidesWith(t2_dieingSprite, true)&& !catchedT2){
                			catchedT2=true;
                			tom2.setPumped();
                		}
                		break;
                	}
                	i--;
                }
                
                
                if(i==0){
                	switch(LUMM2_Level.level){
                	case 1:
                		if(dragon1.isDead()&&dragon1.isVisible()){
                			dragon1Sprite.setPosition(600, 600);
                			d1_dieingSprite.setPosition(700, 700);
                			dragon1.setVisible(false);
                			deathCounter++;
                			score += 400;
                		}
                		if(tom1.isDead()&&tom1.isVisible()){
                			tom1Sprite.setPosition(600, 600);
                			t1_dieingSprite.setPosition(700, 700);
                			tom1.setVisible(false);
                			deathCounter++;
                			score += 400;
                		}
                		break;
                	case 2:
                		if(dragon1.isDead()&&dragon1.isVisible()){
                			dragon1Sprite.setPosition(600, 600);
                			d1_dieingSprite.setPosition(700, 700);
                			dragon1.setVisible(false);
                			deathCounter++;
                			score += 400;
                		}
                		if(tom1.isDead()&&tom1.isVisible()){
                			tom1Sprite.setPosition(600, 600);
                			t1_dieingSprite.setPosition(700, 700);
                			tom1.setVisible(false);
                			deathCounter++;
                			score += 400;
                		}
                		if(tom2.isDead()&&tom2.isVisible()){
                			tom2Sprite.setPosition(600, 600);
                			t2_dieingSprite.setPosition(700, 700);
                			tom2.setVisible(false);
                			deathCounter++;
                			score += 400;
                		}
                		break;
                	case 3:
                		if(dragon1.isDead()&&dragon1.isVisible()){
                			dragon1Sprite.setPosition(600, 600);
                			d1_dieingSprite.setPosition(700, 700);
                			dragon1.setVisible(false);
                			deathCounter++;
                			score += 400;
                		}
                		if(tom1.isDead()&&tom1.isVisible()){
                			tom1Sprite.setPosition(600, 600);
                			t1_dieingSprite.setPosition(700, 700);
                			tom1.setVisible(false);
                			deathCounter++;
                			score += 400;
                		}
                		if(tom2.isDead()&&tom2.isVisible()){
                			tom2Sprite.setPosition(600, 600);
                			t2_dieingSprite.setPosition(700, 700);
                			tom2.setVisible(false);
                			deathCounter++;
                			score += 400;
                		}
                		if(dragon2.isDead()&&dragon2.isVisible()){
                			dragon2Sprite.setPosition(600, 600);
                			d2_dieingSprite.setPosition(700, 700);
                			dragon2.setVisible(false);
                			deathCounter++;
                			score += 400;
                		}
                		break;
                	}
                	catchedD1=false;
                	catchedT1=false;
                	catchedD2=false;
                	catchedT2=false;
                    move = 0;
                    pumpeSprite.setPosition(1000,1000);
                }
                
                detectCollision(); 
        }
        
        /**
         * prueft ob der Spieler von einem Gegner beruehrt wird
         * wenn ja, wird die methode dugDieing() aufgerufen
         * */
        private static void detectCollision() {
                switch (LUMM2_Level.level){
                case 1:
                        if ((dugSprite.collidesWith(dragon1Sprite, true)&&!dragon1.isStunned()) ||
                                        (dugSprite.collidesWith(tom1Sprite, true)&&!tom1.isStunned())) 
                                dugDieing();
                        break;
                case 2:
                        if ((dugSprite.collidesWith(dragon1Sprite, true)&&!dragon1.isStunned()) ||
                        		(dugSprite.collidesWith(tom1Sprite, true)&&!tom1.isStunned()) ||
                        		(dugSprite.collidesWith(tom2Sprite, true)&&!tom2.isStunned()))
                                dugDieing();
                        break;
                case 3:
                        if ((dugSprite.collidesWith(dragon1Sprite, true)&&!dragon1.isStunned()) ||
                        		(dugSprite.collidesWith(dragon2Sprite, true)&&!dragon2.isStunned()) ||
                                (dugSprite.collidesWith(tom1Sprite, true)&&!tom1.isStunned()) ||
                                (dugSprite.collidesWith(tom2Sprite, true)&&!tom2.isStunned()))
                                dugDieing();
                        break;
                }
        }

        /**
         * schaut ob eine Taste gedrueckt wird
         * je nach dem welche taste gedruckt wird, wird move geaendert
         * hier wird auch der sound bei der bewegung des spielers erzeugt
         * */
        private static void calculatePosition() {
                if (LUMM2_Canvas.up && move != moveUp && i==0 && posY>30) {
                        i = 5;
                        move = moveUp;
                        look = moveUp;
                        dugSprite.setTransform(Sprite.TRANS_ROT270);
                        if(!mute){
                        	try{
                            	j=j%2;
                            	if(j==0)Manager.playTone(tone, 3, 100);
                            	else Manager.playTone(tone+2, 3, 100);
                            	j++;
                            
                        	}catch(MediaException e){}
                        }
                }
                else if(LUMM2_Canvas.down && move !=moveDown && i==0 && posY<240){
                        i = 5;
                        move = moveDown;
                        look = moveDown;
                        dugSprite.setTransform(Sprite.TRANS_ROT90);
                        if(!mute){
                        	try{
                            	j=j%2;
                            	if(j==0)Manager.playTone(tone, 3, 100);
                            	else Manager.playTone(tone+2, 3, 100);
                            	j++;
                            
                        	}catch(MediaException e){}
                        }
                }
                else if(LUMM2_Canvas.left && move !=moveLeft && i==0 && posX>0){
                        i = 5;
                        move = moveLeft;
                        look = moveLeft;
                        dugSprite.setTransform(Sprite.TRANS_MIRROR);
                        if(!mute){
                        	try{
                            	j=j%2;
                            	if(j==0)Manager.playTone(tone, 3, 100);
                            	else Manager.playTone(tone+2, 3, 100);
                            	j++;
                            
                        	}catch(MediaException e){}
                        }
                }
                else if(LUMM2_Canvas.right && move !=moveRight && i==0 && posX <210){
                        i = 5;
                        move = moveRight;
                        look = moveRight;
                        dugSprite.setTransform(Sprite.TRANS_NONE);
                        if(!mute){
                        	try{
                            	j=j%2;
                            	if(j==0)Manager.playTone(tone, 3, 100);
                            	else Manager.playTone(tone+2, 3, 100);
                            	j++;
                            
                        	}catch(MediaException e){}
                        }
                }
                else if(LUMM2_Canvas.fire && i==0 && move != fire) {
                	switch(look){
            		case moveLeft:
            			if(currentCol-1<0){}
            			else if(background.getCell(currentCol-1, currentRow)==8 || background.getCell(currentCol-1, currentRow)==9 ||
            					background.getCell(currentCol-1, currentRow)==10||background.getCell(currentCol-1, currentRow)==11||
            					background.getCell(currentCol-1, currentRow)==12||background.getCell(currentCol-1, currentRow)==13){
            				pumpeSprite.setTransform(Sprite.TRANS_MIRROR);
            				pumpeSprite.setPosition(posX-30, posY);
            			}
            			break;
            		case moveUp:
            			if(currentRow-1<0){}
            			else if(background.getCell(currentCol, currentRow -1)==8 || background.getCell(currentCol, currentRow -1)==9 ||
            					background.getCell(currentCol, currentRow -1)==10||background.getCell(currentCol, currentRow -1)==11||
            					background.getCell(currentCol, currentRow -1)==12||background.getCell(currentCol, currentRow -1)==13){
            				pumpeSprite.setTransform(Sprite.TRANS_ROT270);
            				pumpeSprite.setPosition(posX, posY-30);
            			}
            			break;
            		case moveRight:
            			if(currentCol+1 >7){}
            			else if(background.getCell(currentCol+1, currentRow)==8 || background.getCell(currentCol+1, currentRow)==9 ||
            					background.getCell(currentCol+1, currentRow)==10||background.getCell(currentCol+1, currentRow)==11||
            					background.getCell(currentCol+1, currentRow)==12||background.getCell(currentCol+1, currentRow)==13){
            				pumpeSprite.setTransform(Sprite.TRANS_NONE);
            				pumpeSprite.setPosition(posX+30, posY);
            			}
            			break;
            		case moveDown:
            			if(currentRow+1 >8){}
            			else if(background.getCell(currentCol, currentRow +1)==8 || background.getCell(currentCol, currentRow+1)==9 ||
            					background.getCell(currentCol, currentRow +1)==10||background.getCell(currentCol, currentRow +1)==11||
            					background.getCell(currentCol, currentRow +1)==12||background.getCell(currentCol, currentRow +1)==13){
            				pumpeSprite.setTransform(Sprite.TRANS_ROT90);
            				pumpeSprite.setPosition(posX, posY+30);
            			}
            			break;
            		}
                	move = fire;
                	i=5;
                }
        }
        
        /**
         * erstellt den Hintergrund eines Levels
         * form des Hintergrundes wird aus der Klasse LUMM2_Level gelsen
         * ruft die methode createEnemies() auf 
         * */
        private static void createBackground() {        
                switch(LUMM2_Level.level){
                case 1:
                        cells = LUMM2_Level.LEVEL1;
                        createEnemies(1);
                        break;
                case 2:
                        cells = LUMM2_Level.LEVEL2;
                        createEnemies(2);
                        break;
                case 3:
                        cells=LUMM2_Level.LEVEL3;
                        createEnemies(3);
                        break;
                }
                for (int i = 0; i < cells.length; i++) {
                        int column = i % 8;
                        int row = (i - column)/8;
                        background.setCell(column, row, cells[i]);
                }
                background.setPosition(0, 0);           
        }

        /**
         * erstellt die Gegner Klassen und setzt die gegner auf anfangsposition
         * */
        private static void createEnemies(int level) {
                switch (level){
                	case 1: 
                        dragon1Sprite.setFrame(0);
                        dragon1Sprite.setPosition(0, 240);
                        d1_dieingSprite.setPosition(700, 700);
                        dragon1 = new Dragon(0, 240, dragon1Sprite, d1_dieingSprite);
                
                        tom1Sprite.setFrame(0);
                        tom1Sprite.setPosition(210, 120);
                        t1_dieingSprite.setPosition(700, 700);
                        tom1 = new Tom(210, 120, tom1Sprite,  t1_dieingSprite);
                        
                        rock1 = new Rock(30,60);
                        layerManager.append(rock1.rocksprite);
                        break;
                	case 2: 
                		dragon1.reset(0,240);
                		
                		tom1.reset(210, 120);
                        
                		tom2Sprite.setFrame(0);
                		tom2Sprite.setPosition(30,90);
                        t2_dieingSprite.setPosition(700, 700);
                        tom2 = new Tom(30, 90, tom2Sprite,  t2_dieingSprite);
                        break;
                	case 3: 
                		dragon1.reset(0,240);
                		
                		tom1.reset(210, 120);
  
                		tom2.reset(30, 90);
                        
                		dragon2Sprite.setFrame(0);
                		dragon2Sprite.setPosition(150,60);
                        d2_dieingSprite.setPosition(700, 700);
                		dragon2 = new Dragon(150,60, dragon2Sprite, d2_dieingSprite);
                        break;
                }
        }

        /**
         * ladet Image
         * 
         * @param s
         * 			Pfad des bildes
         * @return
         * 			Gibt das geladene Bild in Image Format zurueck oder Null wenn es nicht funktioniert hat
         * */
        private static Image loadImage(String s) {
                try {
                        return Image.createImage(s);
                } catch (Exception e) {
                        System.out.println("Fehler beim Laden vom Bild:"+s);
                }
                return null;
        }
         
        /**
         * wenn der Spieler von einem Gegner beruehrt wird
         * dem Spieler wird ein leben abgezogen und er wird auf anfangsposition gesetzt
         * wenn der spieler 0 leben hat ist das Spiel vorbei, und der gameState wird auf GAME OVER gesetzt
         * */
        public static void dugDieing(){
                //TODO: "sterbe"-dug-frames!!
                if (dugLives > 0) { 
                        dugLives --;
                        posX = 0;
                        posY = 0;
                        dugSprite.setFrame(0);
                        dugSprite.setTransform(Sprite.TRANS_NONE);
                        look = moveRight;
                        dugSprite.setPosition(0, 0);
                        i = 0;
                        j=0;
                } else if (dugLives == 0){
                	lose=53;
                	currentGameState = GAME_OVER;
                	reset();
                }
                
        }
        
        /**
         * fuellt beim initialisieren des Spieles den Highscore mit vorgefertigten eintraegen
         * */
        public static void fillHighscores(){

               rec.addScore(200, "Peter Pan");
               rec.addScore(100, "Max Muster");
               rec.addScore(300, "Andrea Anders");
        }

}